package cn.iocoder.ny.module.mall.service.impl;

import cn.iocoder.ny.module.mall.controller.admin.product.dto.MallProductListDTO;
import cn.iocoder.ny.module.mall.domain.MallProductSpecification;
import cn.iocoder.ny.module.mall.mapper.MallProductSpecificationMapper;
import cn.iocoder.ny.module.mall.service.IMallProductSpecificationSerivce;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author luchunhong
 * @date 2024/08/06/ 11:41
 * @description:
 */
@Service
public class MallProductSpecificationSerivceImpl extends ServiceImpl<MallProductSpecificationMapper, MallProductSpecification> implements IMallProductSpecificationSerivce {
    @Autowired
    private MallProductSpecificationMapper mallProductSpecificationMapper;


    @Override
    public boolean upadteProductStock(MallProductListDTO mallProductListDTO) {
        // 修改库存
        List<MallProductSpecification> mallProductSpecifications = mallProductListDTO.getMallProductSpecifications();
        if (mallProductSpecifications != null) {
            updateBatchById(mallProductSpecifications);
            return true;
        }
        return false;
    }


    //更新销量
    @Override
    public boolean updateProductSell(MallProductListDTO mallProductListDTO) {

       return  lambdaUpdate().setSql("volume = volume  + "+ mallProductListDTO.getBuyCount())
                    .eq(MallProductSpecification::getId,mallProductListDTO.getSpecificationId())
                    .update();
    }

    // reduce inventory
    @Override
    public boolean updateProductStockBySQL(MallProductListDTO mallProductListDTO) {
        return  lambdaUpdate().setSql("stock = stock - " +  mallProductListDTO.getStock())
                .eq(MallProductSpecification::getId,mallProductListDTO.getSpecificationId())
                .update();
    }

    @Override
    public boolean updateProductStockBySQLADD(MallProductListDTO mallProductListDTO) {
        return  lambdaUpdate().setSql("stock = stock + " +  mallProductListDTO.getStock())
                .eq(MallProductSpecification::getId,mallProductListDTO.getSpecificationId())
                .update();
    }

    //todo 扣口库2 reduce inventory 2 condition
    @Override
    public boolean reduceProductStockBySQLCondition(MallProductListDTO mallProductListDTO) {
        return  lambdaUpdate().setSql("stock = stock - " +  mallProductListDTO.getStock())
                .eq(MallProductSpecification::getId,mallProductListDTO.getSpecificationId())
                //玩不起来
                .update();
    }


    //暂时用不到
    @Override
    public int updateStockBySpecIdAndOrderStatus(Integer stock,Long specificationId,Long orderId){
        return mallProductSpecificationMapper.updateStockBySpecIdAndOrderStatus(stock,specificationId,orderId);
    }


}
